package com.haogu.controller;

import com.haogu.entity.*;
import com.haogu.service.impl.ContractService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value = "contract")
public class ContractController {
    @Resource
    private ContractService contractService;
    @RequestMapping(value = "/toContract")
    public String toContract(){
        return "contract/index_contract";
    }
    @RequestMapping(value = "/toList")
    public String toList(){
        return "contract/list_contract";
    }

    /**
     * 分页搜索
     * @param page
     * @param rows
     * @param contractName
     * @param contractOne
     * @param contractTwo
     * @return
     */
    @RequestMapping(value = "/listPage")
    @ResponseBody
    public Map<String,Object> list(HttpSession session,Integer page,Integer rows,String contractName,String contractOne,String contractTwo){
        Map<String,Object> map = new HashMap<>();
        Map<String,Object> map1 = new HashMap<>();
        Map<String,Object> map2 = new HashMap<>();
        Object contractsDay =session.getAttribute("contracts");
        Object contractsList = session.getAttribute("contractsList");
        //"我"创建、负责的
        if (contractsList != null){
            map1.put("result",contractsList);
            map2.put("data",map1);
            session.removeAttribute("contractsList");
            return map2;
        }
        //今日、周、月签约
        if (contractsDay != null){
            map1.put("result",contractsDay);
            map2.put("data",map1);
            session.removeAttribute("contracts");
            return map2;
        }
        if ("customerName".equals(contractOne)){
                map.put("customerName",contractName);
            }
            if ("contactsName".equals(contractOne)){
                map.put("contactsName",contractName);
            }
            if ("userName".equals(contractOne)){
                map.put("userName",contractName);
            }
        map.put("contractTwo",contractTwo);
        map.put("offset",(page-1)*rows);
        map.put("pageSize",rows);
        List<VContract> contracts = contractService.listPage(map);
        int count = contractService.selectCount();
        map1.put("result",contracts);
        map1.put("totalRows",count);
        map2.put("data",map1);
        return map2;
    }

    /**
     * 添加页面
     * @return
     */
    @RequestMapping(value = "/toAdd")
    public String toAdd(){
        return "contract/add_contract";
    }
    @RequestMapping(value = "/add")
    @ResponseBody
    public Map<String,Object> addMap(){
        List<Business> businesses = contractService.listBusiness();
        Map<String,Object> map = new HashMap<>();
        map.put("data",businesses);
        return map;
    }

    /**
     * 保存添加
     * @param contract
     * @return
     */
    @RequestMapping(value = "/keepAdd")
    @ResponseBody
    public Map<String,Object> keepAdd(Contract contract){
        Map<String,Object> map = new HashMap<>();
        int i = contractService.addContract(contract);
        if (i == 0){
            map.put("success",false);
        }else {
            map.put("success",true);
        }
        return map;
    }

    /**
     * 逻辑删除
     * @param ids
     * @return
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Map<String,Object> delete(Integer[] ids){
        Map<String,Object> map = new HashMap<>();
        boolean flag = true;
        for (int i = 0; i < ids.length; i++){
            int k = contractService.deleteContract(ids[i]);
            if (k == 0){
                flag = false;

            }
            if (flag){
                map.put("success",true);
                map.put("message","删除成功");
            }else {
                map.put("success",false);
                map.put("message","删除失败");
            }
        }
        return map;
    }

    /**
     * 跳转修改页面
     * @param modelMap
     * @param id
     * @return
     */
    @RequestMapping(value = "/toEdit")
    public String toEdit(ModelMap modelMap,Integer id){
        Contract contract = contractService.selectContractById(id);
        modelMap.put("contract",contract);
        return "contract/edit_contract";
    }

    /**
     * 修改页面
     * @param contract
     * @return
     */
    @RequestMapping("/edit")
    @ResponseBody
    public Map<String,Object> ListBusiness(Contract contract){
        Map<String,Object> map = new HashMap<>();
        int i = contractService.updateContract(contract);
        if (i == 0){
            map.put("success",true);
            map.put("message","修改失败");
        }else {
            map.put("success",false);
            map.put("message","修改成功");
        }

        return map;
    }
    @RequestMapping("toSee")
    public String seeContract(ModelMap modelMap,@RequestParam("id") Integer id){
        Contract contract = contractService.selectContractById(id);
        modelMap.put("contract",contract);
        return "contract/see_contract";
    }
    /**
     * 今日、周、月签约
     * @param session
     * @param page
     * @param rows
     * @param type
     * @return
     */
    @RequestMapping(value = "/todaySigned")
    public String todayCreate(HttpSession session, @RequestParam(value = "page",defaultValue = "1") Integer page,
                              @RequestParam(value = "rows",defaultValue = "10") Integer rows,@RequestParam("type") String type){
        Map<String,Object> map = new HashMap();
        map.put("offset",(page-1)*rows);
        map.put("pageSize",rows);
        map.put("type",type);
        List<VContract> contracts = contractService.selectDay(map);
        session.setAttribute("contracts",contracts);
        return "contract/list_contract";
    }

    /**
     * "我"创建的、负责的
     * @param session
     * @param page
     * @param rows
     * @param user
     * @return
     */
    @RequestMapping(value = "my")
    public String user(HttpSession session, @RequestParam(value = "page",defaultValue = "1") Integer page,
                       @RequestParam(value = "rows",defaultValue = "10") Integer rows,@RequestParam("user") String user){
        Map<String,Object> map = new HashMap();
        map.put("offset",(page-1)*rows);
        map.put("pageSize",rows);
        map.put("user",user);
        User user1 = (User)session.getAttribute("user");
        map.put("username",user1.getUsername());
        List<VContract> contractsList = contractService.selectByUser(map);
        session.setAttribute("contractsList",contractsList);
        return "contract/list_contract";
    }
}
